/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.helper;

import de.marantis.app.wow.gfi.beans.GuildCharacterBean;
import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 * This class analyze the [armoryContent] and list all members in a guild<br> 
 * <br> 
 * Expl: http://eu.battle.net/wow/de/guild/gilneas/[GUILD]/roster
 */
public class ArmoryGuildHelper {

    private final static Logger log = Logger.getLogger(ArmoryGuildHelper.class);

    /*
     * get all member infos from armory page. the result will be added
     * to the dyn. array list<br>
     * <br>
     * this function returns -1 by an error or the count of the member rows.
     */
    public static int getPageGuildMember(String armoryContent, ArrayList<GuildCharacterBean> chars) {
        try {

            int posTR;
            int posTD;
            int pos;
            int pos2;

            int countTR;
            int countTD;

            String search;
            String search2;

            if (armoryContent == null) {
                return -1;
            }

            if (chars == null) {
                return -1;
            }

            //*** jump to the member table ***
            search = "<div id=\"roster\" class=\"table\">";
            pos = armoryContent.indexOf(search);
            if (pos < 0) {
                return -1;
            }

            //*** analyze ROWS ************************************************************************

            posTR = pos;
            countTR = 0;
            //System.out.println("posTR init = " + posTR);

            //*** run in all TR  ***
            while (true) {

                //*** jump to the next row (TR) ***
                search = "<tr";
                posTR = armoryContent.indexOf(search, posTR + 1);
                //System.out.println("posTR suche = " + posTR);
                if (posTR < 0) {
                    break; // no more rows? the jump out
                }

                countTR += 1;
                GuildCharacterBean aChar = new GuildCharacterBean();


                //*** analyze COLUMNS *******************************************************************

                countTD = 0;
                posTD = posTR;
                //System.out.println("posTD init = " + posTD);

                //*** run in all TD ***
                while (true) {

                    //*** jump to the next column (TD) ***
                    search = "<td";
                    posTD = armoryContent.indexOf(search, posTD + 1);
                    //System.out.println("posTD suche = " + posTD);
                    if (posTD < 0) {
                        break; // no more cols then out
                    }

                    //*** character col *****************************************************************
                    if (countTD == 0) {
                        //*** jump to the CHAR-LINK ***
                        search = "href=\"";
                        pos = armoryContent.indexOf(search, posTD);
                        //System.out.println("pos suche = " + pos);
                        if (pos < 0) {
                            return -1; // error: there has to be a link
                        }

                        //*** jump to the CHAR-LINK-END ***
                        search2 = "\"";
                        pos2 = armoryContent.indexOf(search2, pos + search.length());
                        if (pos2 < 0) {
                            return -1; // error: there has to be a link end
                        }

                        //*** get the LINK-TEXT ***
                        String strCharLink = armoryContent.substring(pos + search.length(), pos2);
                        strCharLink = strCharLink.trim();


                        //*** jump to the LINK TEXT START ***
                        search = ">";
                        pos = armoryContent.indexOf(search, pos2);
                        if (pos < 0) {
                            return -1; // error: there has to be a link text
                        }

                        //*** Sprung auf den LINK TEXT END ***
                        search2 = "<";
                        pos2 = armoryContent.indexOf(search2, pos);
                        if (pos2 < 0) {
                            return -1; // error: there has to be a link text end
                        }

                        //*** get the LINK-TEXT ***
                        String strCharName = armoryContent.substring(pos + search.length(), pos2);
                        strCharName = strCharName.trim();

                        //*** control ***
                        if (strCharLink == null || strCharName == null) {
                            return -1;
                        }

                        aChar.setCharArmoryLink(strCharLink);
                        aChar.setCharName(strCharName);

                        //*** level column **************************************************************
                    } else if (countTD == 3) {

                        //*** jump to the LVL START ***
                        search = ">";
                        pos = armoryContent.indexOf(search, posTD);
                        if (pos < 0) {
                            return -1; // error: there has to be a level
                        }

                        //*** jump to the LVL-END ***
                        search2 = "<";
                        pos2 = armoryContent.indexOf(search2, pos);
                        if (pos2 < 0) {
                            return -1; // error: there has to be a level end
                        }

                        //*** get the LINK-TEXT ***
                        String strCharLevel = armoryContent.substring(pos + search.length(), pos2);
                        strCharLevel = strCharLevel.trim();
                        int charLevel = CommonHelper.String2Int(strCharLevel);

                        //*** control ***
                        if (charLevel < 0) {
                            return -1;
                        }

                        aChar.setCharLevel(charLevel);

                        //*** guild rank column **************************************************************
                    } else if (countTD == 4) {

                        //*** jump to the RANK START ***
                        search = "data-raw=\"";
                        pos = armoryContent.indexOf(search, posTD);
                        if (pos < 0) {
                            return -1; // error: there has to be a guild rank
                        }

                        //***  jump to the RANK END ***
                        search2 = "\"";
                        pos2 = armoryContent.indexOf(search2, pos + search.length());
                        if (pos2 < 0) {
                            return -1; // error: there has to be a guild rank end
                        }

                        //*** get the RANK-TEXT ***
                        String strCharRank = armoryContent.substring(pos + search.length(), pos2);
                        strCharRank = strCharRank.trim();
                        int guildRank = CommonHelper.String2Int(strCharRank);

                        //*** control ***
                        if (guildRank < 0) {
                            return -1;
                        }

                        aChar.setGuildRank(strCharRank);

                    }

                    //*** TD counter inc ***
                    countTD += 1;

                    //*** if we have finished al important cols then go out ***
                    if (countTD > 4) {
                        posTR = posTD; // search for the next TR starting last TD
                        break;
                    }
                }

                //*** save the character in the array;
                chars.add(aChar);
            }

            return countTR;

        } catch (Exception e) {
            log.error(e);
            return -1;
        }
    }
}
